var shopcarVue = new Vue({
	el: "#app",
	data: {
		isExcute: false,
		isBottom: false,
		page: 1,
		shopCars: [],
		allCheck: false,
		allPrice: 0,
		allPoints: 0,
		allCount: 0,
		edit: false,
		del_Disabled:false,
	},
	methods: {
		checked: function(t) {
			if (t.over && !this.edit) {
				return;
			} 
			var flag = !t.check;
			t.check = flag;
			this.calcMoney();
			if(this.edit){
				this.$forceUpdate();
			}
		},
		calcMoney: function() {
			if(this.edit){
				return;
			}
			var allCount = 0;
			var priceTotal = 0.0;
			var pointsTotal = 0;
			this.shopCars.forEach(function(e) {
				if (e.check && !e.over) {
					priceTotal = parseFloat(priceTotal) + parseFloat(e.price) * parseInt(e.count);
					pointsTotal = pointsTotal + parseInt(e.points) * parseInt(e.count);
					allCount++;
				}
			});
			this.allPrice = priceTotal.toFixed(2);
			this.allPoints = pointsTotal;
			this.allCount = allCount;
		},
		addCount: function(item) {
			if (item.over) {
				return;
			}
			if (item.count >= item.stockCount) {
				wx.showToast({
					title: "亲！库存仅剩" + item.stockCount + "件，抓紧时间抢购吧！"
				})
				return;
			}
			item.count++;
			this.calcMoney();
		},
		reduceCount: function(item) {
			if (item.count == 1) {
				return;
			}
			item.count--;
			this.calcMoney();
		},
		getList: function() {
			var t = this;
			if (this.isBottom) return;
			wx.showLoading({
				title: "加载中",
				mask: !0
			}), this.$http.post(baseUrl + "shopCar/list", {
				currentPage: this.page,
				pageSize: 10
			}).then(function(e) {
				wx.hideLoading();
				var s = e.data;
				if (!s.success) return wx.hideLoading(), wx.showToast({
					title: s.description,
					icon: "none",
					duration: 2e3,
					mask: !0
				}), !1;
				if (t.mall_num = s.total, 0 == s.data.length && t.page > 1) return wx.showToast({
					title: "已经到最底下了",
					icon: "none",
					duration: 1e3,
					mask: !0
				}), t.isBottom = true, !1;
				if (s.data.length > 0 && 1 == t.page) t.shopCars = s.data;
				else {
					s.data.forEach(function(e) {
						t.shopCars.push(e);
					});
				}
			}).catch(function(t) {
				wx.hideLoading(), wx.showToast({
					title: "网络错误，请检查网络",
					icon: "none",
					duration: 2e3,
					mask: !0
				});
			});
		},
		collect: function() {
			var removeList = new Array();
			var t = this;
			if(t.del_Disabled){
				return;
			}
			t.shopCars.forEach(function(e) {
				if (e.check) {
					removeList.push(e.productGuid);
				}
			});
			wx.showLoading({
				title: "加载中",
				mask: !0
			}),this.$http.post(baseUrl+"shopCar/add/collects", {
				productGuid: JSON.stringify(removeList)
			}).then(function(e) {
				wx.hideLoading();
				var s = e.data;
				if (!s.success) return wx.hideLoading(), t.del_Disabled = !1, wx.showToast({
					title: s.description,
					icon: "none",
					duration: 2e3,
					mask: !0
				}), !1;
				wx.showToast({
					title: "移入收藏成功!",
					icon: "success",
					duration: 2e3,
					mask: !1,
					success: function(t) {}
				}), setTimeout(function() {
					t.shopCars = [], t.del_Disabled = !1, t.page = 1,t.isBottom=false, t.getList();
				}, 1500);
			}).catch(function(e) {
				wx.hideLoading(), t.del_Disabled = !1, wx.showToast({
					title: "网络错误，请检查网络",
					icon: "none",
					duration: 2e3,
					mask: !0
				});
			});
		},
		remove: function() {
			var removeList = new Array();
			var t = this;
			if(t.del_Disabled){
				return;
			}
			t.shopCars.forEach(function(e) {
				if (e.check) {
					removeList.push(e.shopCarGuid);
				}
			});
			wx.showLoading({
				title: "加载中",
				mask: !0
			}),this.$http.post(baseUrl+"shopCar/remove", {
				shopCarGuid: JSON.stringify(removeList)
			}).then(function(e) {
				wx.hideLoading();
				var s = e.data;
				if (!s.success) return wx.hideLoading(), t.del_Disabled = !1, wx.showToast({
					title: s.description,
					icon: "none",
					duration: 2e3,
					mask: !0
				}), !1;
				wx.showToast({
					title: "删除成功",
					icon: "success",
					duration: 2e3,
					mask: !1,
					success: function(t) {}
				}), setTimeout(function() {
					t.shopCars = [], t.del_Disabled = !1, t.page = 1,t.isBottom=false, t.getList();
				}, 1500);
			}).catch(function(e) {
				wx.hideLoading(), t.del_Disabled = !1, wx.showToast({
					title: "网络错误，请检查网络",
					icon: "none",
					duration: 2e3,
					mask: !0
				});
			});
		},
		allChecked: function() {
			this.allCheck = !this.allCheck;
			var flag = this.allCheck;
			var edit = this.edit;
			this.shopCars.forEach(function(e) {
				if (e.over && !edit ) {
					return;
				}
				e.check = flag;
			});
			this.calcMoney();
		},
		changeEdit:function(){
			var t = this;
			t.edit = !t.edit;
			t.allCheck = false;
			t.shopCars.forEach(function(e) {
				e.check = false;
			});
			t.allPoints = 0;
			t.allPrice = 0;
			t.allCount = 0;
		},
		handleScroll: function() {
			// 防止重复
			if (this.isExcute) {
				return;
			}
			if (this.isBottom) {
				return;
			}
			var flag = srollEvent.isReachBottom();
			if (!flag) {
				return;
			}
			this.isExcute = true;
			this.page++;
			this.getList();
		},
		toCash:function(){
			var submitList = new Array();
			var t = this;
			t.shopCars.forEach(function(e) {
				if (e.check) {
					submitList.push(e.shopCarGuid);
				}
			});
			if(submitList.length == 0){
				wx.showToast({
					title: "请选择待支付的商品",
					icon: "none",
					duration: 2e3,
					mask: !0
				});
				return; 
			}
			Jump.to(page.order.preview,"ids="+JSON.stringify(submitList));
		}
	},
	mounted: function() {
		window.addEventListener("scroll", this.handleScroll, {
			passive: false
		});
	},
	created: function() {
		this.getList();
	}

});
